<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@ include file="/WEB-INF/jsp/component/common.jsp" %>
<!DOCTYPE html>
<html>
<head>
    <title>经销商新增</title>
    <meta charset="utf-8">
    <!-- Bootstrap Core CSS -->
    <link href="${css}/bootstrap.min.css" rel="stylesheet"/>
</head>
<body style="width: 98%">
    <form id="myForm" class="form-horizontal" method="post" >
        <input type="hidden" id="areaCode" name="areaCode" value="" />
        <div class="form-group">
            <label for="companyName" class="col-sm-2 control-label">经销商名称</label>
            <div class="col-sm-10">
                <input type="text" class="form-control" id="companyName" name="companyName"
                       placeholder="请输入经销商名称">
            </div>
        </div>
        <div class="form-group">
            <label for="largeArea" class="col-sm-2 control-label">所属地区</label>
            <div class="col-sm-3">
                <select id="largeArea" class="form-control">
                    <option value="">大区</option>
                    <c:forEach items="${largeAreaList}" var="largeArea">
                        <option value="${largeArea.largeAreaCode}">${largeArea.largeAreaName}</option>
                    </c:forEach>
                </select>
            </div>
            <div class="col-sm-3">
                <select id="provinceArea" class="form-control">
                    <option value="">省</option>
                </select>
            </div>
            <div class="col-sm-4">
                <select id="cityArea" class="form-control">
                    <option value="">市</option>
                </select>
            </div>
        </div>
        <div class="form-group">
            <label for="companyAddress" class="col-sm-2 control-label">公司地址</label>
            <div class="col-sm-10">
                <input type="text" class="form-control" id="companyAddress" name="companyAddress"
                       placeholder="请输入公司地址">
            </div>
        </div>
        <div class="form-group">
            <label for="level" class="col-sm-2 control-label">经销商等级</label>
            <div class="col-sm-10">
                <select id="level" name="level" class="form-control">
                    <option value="" >请选择...</option>
                    <option value="A" >A</option>
                    <option value="B" >B</option>
                    <option value="C" >C</option>
                </select>
            </div>
        </div>
        <div class="form-group">
            <label for="linkMan" class="col-sm-2 control-label">联系人</label>
            <div class="col-sm-10">
                <input type="text" class="form-control" id="linkMan" name="linkMan"
                       placeholder="请输入联系人">
            </div>
        </div>
        <div class="form-group">
            <label for="linkPhine" class="col-sm-2 control-label">联系电话</label>
            <div class="col-sm-10">
                <input type="text" class="form-control" id="linkPhine" name="linkPhine"
                       placeholder="请输入联系电话">
            </div>
        </div>
    </form>

    <!-- jQuery -->
    <script src="${js}/jquery-1.9.1.min.js"></script>
    <!-- Bootstrap Core JavaScript -->
    <script src="${js}/bootstrap.min.js"></script>

    <script src="${js}/bootbox.js"></script>

    <script type="text/javascript">
        $(function () {
            $("#largeArea").change(function() {
                if(this.value==null || this.value==""){
                    resetArea(0);
                }else{
                    getLowerAreaList("provinceArea",this.value,0);
                    resetArea(1);
                }
            });
            $("#provinceArea").change(function() {
                if(this.value==null || this.value==""){
                    resetArea(1);
                }else{
                    getLowerAreaList("cityArea",this.value,1);
                    resetArea(2);
                }
            });

            /**
             * 后台获取下级列表
             * */
            function getLowerAreaList(id,code,level){
                $.ajax({
                    url:'${ctx}/area/getLowerAreaList',
                    type:'post',
                    data:'code='+code+"&level="+level,
                    async : false, //默认为true 异步
                    error:function(){
                        alert('error');
                    },
                    success:function(data){
                        var objs = document.getElementById(id);
                        objs.options.length = 1;
                        var area = data;
                        for(var i=0;i<area.length;i++){
                            var childOption = document.createElement("option");
                            childOption.value = area[i].code;
                            childOption.innerHTML = area[i].name;
                            objs.appendChild(childOption);
                        }
                    }
                });
            }
        });

        /**
         * 重置下级菜单
         * */
        function resetArea(level){
            if(level<1){
                var provinceArea = document.getElementById("provinceArea");
                provinceArea.options.length = 1;
            }
            if(level<2){
                var cityArea = document.getElementById("cityArea");
                cityArea.options.length = 1;
            }
//            if(level<3){
//                var countyArea = document.getElementById("countyArea");
//                countyArea.options.length = 1;
//            }
//            if(level<4){
//                var townArea = document.getElementById("townArea");
//                townArea.options.length = 1;
//            }
//            if(level<5){
//                var villageArea = document.getElementById("villageArea");
//                villageArea.options.length = 1;
//            }
        }

        /**
         * 获取最详细地址code
         * */
        function getDetailAreaCode(){
//            var villageArea = $("#villageArea").val();//村code
//            var townArea = $("#townArea").val();//镇code
//            var countyArea = $("#countyArea").val();//县code
            var cityArea = $("#cityArea").val();//市code
            var provinceArea = $("#provinceArea").val();//省code
            var largeArea = $("#largeArea").val();//大区code
//            if(villageArea!=null && villageArea!=""){
//                return villageArea;
//            }else if(townArea!=null && townArea!=""){
//                return townArea;
//            }else if(countyArea!=null && countyArea!=""){
//                return countyArea;
//            }else
            if(cityArea!=null && cityArea!=""){
                return cityArea;
            }else if(provinceArea!=null && provinceArea!=""){
                return provinceArea;
            }else if(largeArea!=null && largeArea!=""){
                return largeArea;
            }else{
                return "";
            }
        }

        function doAdd(){
            $("#areaCode").val(getDetailAreaCode());
            $.ajax({
                url:'${URL_ADM_AGENTCOMPANY_ADD}',
                type:'post',
                data:$('#myForm').serialize(),
                async : false, //默认为true 异步
                error:function(){
                    alert('error');
                },
                success:function(data){
                    window.parent.closeModal("add");
                }
            });
        }

    </script>
</body>
</html>